package com.turboconnard.core;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import com.turboconnard.data.Library;
import com.turboconnard.display.Stage;
import com.turboconnard.events.Event;
import com.turboconnard.events.EventListener;

public class MainThread extends Thread  implements EventListener {
	protected SurfaceHolder surfaceHolder;
	private boolean mRun = false;
	private long oldTime;
	private long _fps = 1000/30; // (60 fps ! );
	private static Context context;
	private Library _library;
	protected Stage stage;

	public MainThread(Context pContext) {
		context = pContext;
		oldTime = System.currentTimeMillis();
		stage = Stage.getInstance();
		
	}	
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}	
	
	@Override
	public void run() {
		while (mRun) {
			long s = System.currentTimeMillis();
			if (s > oldTime + _fps ) {
				_update();
				long p = System.currentTimeMillis();
				oldTime = p;			
				/*feinte pour avoir un fps fixe..ok */
				//	_tick();
			}
		}
	}
	
	protected void _tick() {
		//FPS.getInstance().tick();
	}
	protected void update() {
		
	}
	private void _update(){
		/*
		Stage.getInstance().update();		
		update();*/
	}
	protected void _draw(GL10 gl) {
		//pCanvas.drawARGB(255, 1, 0, 0);
		//_stage.draw(gl);		
	}
	public boolean onKeyUp(int keyCode, KeyEvent msg) {
		boolean handled = false;
		synchronized (surfaceHolder) {
			
		}
		return handled;
	}

	/**
	 * Dump game state to the provided Bundle. Typically called when the
	 * Activity is being suspended.
	 * 
	 * @return Bundle with this view's state
	 */
	public Bundle saveState(Bundle map) {
		synchronized (surfaceHolder) {
			
		}
		return map;
	}

	/**
	 * Used to signal the thread whether it should be running or not. Passing
	 * true allows the thread to run; passing false will shut it down if it's
	 * already running. Calling start() after this was most recently called with
	 * false will result in an immediate shutdown.
	 * 
	 * @param b
	 *            true to run, false to shut down
	 */
	public void setRunning(boolean b) {
		mRun = b;
	}

	/**
	 * Sets the game mode. That is, whether we are running, paused, in the
	 * failure state, in the victory state, etc.
	 * 
	 * @see #setState(int, CharSequence)
	 * @param mode
	 *            one of the STATE_* constants
	 */
	public void setState(int mode) {
		synchronized (surfaceHolder) {
			setState(mode, null);
		}
	}

	/**
	 * Sets the game mode. That is, whether we are running, paused, in the
	 * failure state, in the victory state, etc.
	 * 
	 * @param mode
	 *            one of the STATE_* constants
	 * @param message
	 *            string to add to screen or null
	 */
	public void setState(int mode, CharSequence message) {
		synchronized (surfaceHolder) {
		
		}
	}
	public void setSurfaceSize(int w, int h) {
		synchronized (surfaceHolder) {
			/*
			_stage.width = w;
			_stage.height = h;
			*/
		}
	}
	public void event(Event e){
		
	}
	public static Context getContext() {
		return context;
	}
}
